package com.odata.springboot.starter.odata.edmprovider;


import com.odata.springboot.starter.odata.odata.config.OdataCachedData;
import com.odata.springboot.starter.odata.odata.config.OdataConfig;
import org.apache.olingo.commons.api.edm.FullQualifiedName;
import org.apache.olingo.commons.api.edm.provider.*;
import org.apache.olingo.commons.api.ex.ODataException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * edm元数据
 *
 * @author chenYX
 * @date 2021/6/23 11:23
 * @description
 */
@Component
@ConditionalOnBean(OdataConfig.class)
@Configuration
public class SimpleEdmProvider extends CsdlAbstractEdmProvider {

    @Autowired
    private OdataConfig odataConfig;


    /**
     *
     * @author : yongxiang
     * @description : odata 元数据
     * @param
     * @date : 15:47 2021/6/30
    */
    @Override
    public CsdlEntityContainer getEntityContainer() throws ODataException {
        return odataConfig.getCsdlEntityContainer();
    }


    @Override
    public CsdlEntityContainerInfo getEntityContainerInfo(FullQualifiedName entityContainerName) throws ODataException {
        if (entityContainerName == null || entityContainerName.equals(odataConfig.getContainerFullQualifiedName())){
             CsdlEntityContainerInfo csdlEntityContainerInfo = new CsdlEntityContainerInfo();
             CsdlEntityContainerInfo csdlEntityContainerInfo1 = csdlEntityContainerInfo.setContainerName(odataConfig.getContainerFullQualifiedName());
            return csdlEntityContainerInfo1;
        }
        return null;
    }

    @Override
    public CsdlEntitySet getEntitySet(FullQualifiedName entityContainer, String entitySetName) throws ODataException {
        if (entityContainer.equals(odataConfig.getContainerFullQualifiedName())){
            return OdataCachedData.cachedCsdlEntitySetMap.get(entitySetName);
        }
        return null;
    }

    /**
     * 获取entityType
     * @author : yongxiang
     * @description :
     * @param
     * @date : 14:58 2021/7/1
    */
    @Override
    public CsdlEntityType getEntityType(FullQualifiedName entityTypeName) throws ODataException {
        final CsdlEntityType csdlEntityType = OdataCachedData.cachedCsdlEntityTypeMap.get(entityTypeName.getFullQualifiedNameAsString());
        return csdlEntityType;
    }

    /**
     * 获取元数据
     * @author : yongxiang
     * @description :
     * @param
     * @date : 14:58 2021/7/1
    */
    @Override
    public List<CsdlSchema> getSchemas() throws ODataException {
        return odataConfig.getSchemaList();
    }
}
